Crop

对四维输入张量进行裁剪(Crop)操作。

该算子根据指定的 axis``(起始裁剪维度)和 ``offset``(从 ``axis 开始的各维度起始偏移量),从输入张量中裁剪出一个子区域。offset 数组长度为 4 - axis,其第 i 个元素对应 axis + i 维度的起始位置。

设输入形状为 \([N_{in}, H_{in}, W_{in}, C_{in}]\),输出形状为 \([N_{out}, H_{out}, W_{out}, C_{out}]\),偏移量 \(\text{offset} = [o_0, o_1, \dots]\) (长度为 \(4 - \text{axis}\))。将其扩展为四维偏移 \([p_0, p_1, p_2, p_3]\),其中前 \(\text{axis}\) 个元素为 0,其余依次取自 offset

对输出张量的每个位置 \((n, h, w)\),最内维以连续内存块形式从输入拷贝:

\[\text{output}[n, h, w, :] = \text{input}[n + p_0,\; h + p_1,\; w + p_2,\; p_3 : p_3 + C_{out}]\]

其中:

  • \(n \in [0, N_{out})\)

  • \(h \in [0, H_{out})\)

  • \(w \in [0, W_{out})\)

例如,axis=1, offset=[1,1,0] 时四维偏移为 \([0, 1, 1, 0]\),表示:N 维偏移 0,H 维偏移 1,W 维偏移 1,C 维偏移 0。

输入:
  • input - 输入数据地址。

  • in_shape - 输入张量形状。

  • out_shape - 输出张量形状。

  • offset - 每一维度裁剪开始的偏移量。

  • axis - 裁剪开始的维度。

  • core_mask - 核掩码(仅共享存储版本使用)。

输出:
  • output - 输出地址。

支持平台:

FT78NE MT7004

备注

  • FT78NE 支持 fp32。

  • MT7004 支持 fp16、fp32。

共享存储版本:

void fp_crop_s(float *input, float *output, const int *in_shape, const int *out_shape, int *offset, int axis, int core_mask)
void hp_crop_s(float16 *input, float16 *output, const int *in_shape, const int *out_shape, int *offset, int axis, int core_mask)

C调用示例:

 1// MT7004 示例(共享存储多核,DDR 地址)
 2void TestCropSMCFp32(int* in_shape, int* out_shape, int axis, int* offset_, int core_mask) {
 3    int core_id = get_core_id();
 4    int logic_core_id = GetLogicCoreId(core_mask, core_id);
 5    int core_num = GetCoreNum(core_mask);
 6    float* input = (float*)0x88000000;
 7    float* output = (float*)0x98000000;
 8    int* input_shape = (int*)0xA8000000;
 9    int* output_shape = (int*)0xA8200000;
10    int* offset = (int*)0xA8410000;
11    if (logic_core_id == 0) {
12        memcpy(offset, offset_, sizeof(int) * (4 - axis));
13        memcpy(input_shape, in_shape, sizeof(int) * 4);
14        memcpy(output_shape, out_shape, sizeof(int) * 4);
15    }
16    sys_bar(0, core_num); // 初始化参数完成后进行同步
17    fp_crop_s(input, output, input_shape, output_shape, offset, axis, core_mask);
18}
19
20void main(){
21    int in_shape[4] = {2, 3, 3, 5};
22    int out_shape[4] = {2, 2, 2, 5};
23    int axis = 1;
24    int offset[3] = {1, 1, 0};
25    int core_mask = 0b1111;
26    TestCropSMCFp32(in_shape, out_shape, axis, offset, core_mask);
27}

私有存储版本:

void fp_crop_p(float *input, float *output, const int *in_shape, const int *out_shape, int *offset, int axis)
void hp_crop_p(float16 *input, float16 *output, const int *in_shape, const int *out_shape, int *offset, int axis)

C调用示例:

 1// MT7004 示例(私有存储单核,AM 地址)
 2void TestCropSMCFp32_p(int* in_shape, int* out_shape, int axis, int* offset_) {
 3    float* input = (float*)0x10000000;
 4    float* output = (float*)0x10020000;
 5    int* input_shape = (int*)0x10040000;
 6    int* output_shape = (int*)0x10041000;
 7    int* offset = (int*)0x10042000;
 8
 9    memcpy(offset, offset_, sizeof(int) * (4 - axis));
10    memcpy(input_shape, in_shape, sizeof(int) * 4);
11    memcpy(output_shape, out_shape, sizeof(int) * 4);
12
13    fp_crop_p(input, output, input_shape, output_shape, offset, axis);
14}
15
16void main(){
17    int in_shape[4] = {2, 3, 3, 5};
18    int out_shape[4] = {2, 2, 2, 5};
19    int axis = 1;
20    int offset[3] = {1, 1, 0};
21    TestCropSMCFp32_p(in_shape, out_shape, axis, offset);
22}